
#ifndef ___HMAC_SHA1_H__
#define ___HMAC_SHA1_H__
#pragma once

#include "atlcrypt.h"
#include <atlenc.h>

class HmacSha1
{


public:
	static CString Encrypt( CString const& strData, CString const& strKey)
	{
		CCryptProv prov;
		LRESULT l = prov.Initialize (PROV_RSA_FULL, NULL, NULL, CRYPT_VERIFYCONTEXT) ;
		if (0x80090016L == l)
		{
			prov.Initialize (PROV_RSA_FULL, NULL, MS_DEF_PROV, CRYPT_NEWKEYSET) ;
		}


		std::auto_ptr<KEY_BLOB>   kb (new KEY_BLOB(sKey)) ;

		CCryptImportKey   import_key ;
		import_key.Initialize (prov, (BYTE*)kb.get(), sizeof(KEY_BLOB), CCryptKey::EmptyKey, CRYPT_IPSEC_HMAC_KEY) ;

		HMAC_INFO   HmacInfo ;
		ZeroMemory (&HmacInfo, sizeof(HmacInfo)) ;
		HmacInfo.HashAlgid = CALG_SHA1 ;

		CCryptHMACHash   hmac ;
		hmac.Initialize (prov, import_key) ;
		hmac.SetParam (HP_HMAC_INFO, (BYTE*)&HmacInfo, 0) ;
		hmac.AddData ((const BYTE*)sData.c_str(), (DWORD)sData.length()) ;

		return base64_hmac_result(hmac) ;
	}

};
#endif